package net.lzzy.practicesapi1.services;

import net.lzzy.practicesapi1.entities.User;
import net.lzzy.practicesapi1.repositories.UserProjection;
import net.lzzy.practicesapi1.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//字段注入，最简单，不像其他俩种可以传递参数，无法保证依赖的完成性
//

@Service
public class UserServiceImpl implements UserService {

    private final UserRepository repository;
    @Autowired
    public UserServiceImpl(UserRepository repository) {
        this.repository = repository;
    }


    @Override
    public User check(String userName, String password) {
        return repository.findUserByUserNameAndPassword(userName,password);
    }

    @Override
    public boolean isUserNameOccupied(String userName) {
        return repository.countByUserName(userName)>0;
    }

    @Override
    public User register(User user) {
        return repository.save(user);
    }

    @Override
    public String getSalt(String username) {
        UserProjection projection=repository.findUsersByUserName(username);
        if (projection==null){
            return "";
        }
        return projection.getSalt();
    }
}
